package study.kit.el.zh.table;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import study.kit.el.zh.parse.CellVO;
import study.kit.el.zh.parse.RowVO;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class HeaderUtil {

    // 标题头和对象属性映射
    private static Map<String, String> NAME_FIELD = MapUtil.<String, String>builder()
            .put("季度", "quarter")
            .put("课次", "lessonNo")
            .put("课程名称", "lessonName")
            .put("知识点", "knoPoint")
            .put("切片链接", "cutLinks")
            .put("难度", "difficulty")
//            .put("首三推荐", "f3")
//            .put("重要性（文本说明）", "importance")
//            .put("题库知识图谱", "knoAtlas")
//            .put("切片id", "cutLinkIds")
//            .put("知识图谱id", "knoAtlasIds")
            .build();
    // 禁止行合并标题名列表
    private static List<String> NO_ROW_MERGE_NAME_LT = Arrays.asList("切片链接");
    // 禁止行合并标题名列表
    private static Set<String> NAME_SET = NAME_FIELD.keySet();

    /**
     * 标题数量
     *
     * @return int
     */
    public static int count() {
        return NAME_FIELD.keySet().size();
    }

    /**
     * 是否合法标题
     *
     * @return boolean
     */
    public static boolean isLegal(List<String> headerLt) {
        return CollectionUtil.containsAll(headerLt, NAME_SET);
    }

    /**
     * 获取禁止行合并列号
     *
     * @return List<Integer>
     */
    public static List<Integer> getNoRowMergeColNumLt(RowVO rowVO) {
        return rowVO.getCellVOLt().stream()
                .filter(e -> CollectionUtil.contains(NO_ROW_MERGE_NAME_LT, Convert.toStr(e.getValue())))
                .map(CellVO::getColNum)
                .collect(Collectors.toList());
    }

    /**
     * 获取属性名称
     *
     * @param colName
     * @return String
     */
    public static String getFieldName(String colName) {
        // header name --> field name
        String fieldName = MapUtil.getStr(NAME_FIELD, colName);
        return fieldName;
    }
}
